// pages/product-list/product-list.js
const app = getApp();
const http = require('../../utils/httpUtils.js');
import Toast from '../../component/toast/toast';
import utils from '../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        productList: [],
        pageSize: 10,
        pageIndex: -1,
        hasMore: true,
        flag: 0,
        parentTypeId: 0,
        userId: ''
    },
    getServiceBytype() {
        let that = this;
        if (!that.data.hasMore) return;
        let pageI = ++that.data.pageIndex;
        let data = {
            pageSize: that.data.pageSize,
            pageIndex: pageI,
            parentTypeId: '',
            childTypeIds: ''
        };
        that.setData({
            pageIndex: pageI
        });
        if (this.data.flag == 1) {
            data.parentTypeId = this.data.parentTypeId;
        }
        http.requestGet({
            url: app.globalData.serverAddress.getServiceByType,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        // console.log(JSON.stringify(res.data))
                        let count = res.data.BodyContent.TotalCount;
                        let flag = (that.data.pageIndex + 1) * that.data.pageSize < count;
                        // console.log(flag);
                        var newList = that.data.productList.concat(res.data.BodyContent.List);
                        newList.forEach(function(item, index) {
                            newList[index]._amount = utils.formatAmount(item.Price);
                        });
                        that.setData({
                            productList: newList,
                            hasMore: flag
                        });
                    } else {
                        that.setData({
                            hasMore: false
                        });
                        let msg = res.data.Message;
                        Toast(msg);
                    }
                }
                
            },
            fail: function(res) {
                // console.log(JSON.stringify(res));
                let msg = res.data.Message;
                if(msg){
                    Toast(msg);
                }else{
                    Toast("请求失败，请检查网络！");
                }
            },
            complete: function(res) {
                wx.hideLoading();
            }
        });
    },
    selectItem(e) {
        let index = e.currentTarget.dataset.index;
        let dataObj = this.data.productList[index];
        app.globalData.serviceContent = dataObj.ServiceContent;
        let parmas = JSON.stringify(dataObj).replace(/&nbsp;/g, ' ');
        app.globalData.serviceInfo = JSON.parse(parmas);
        wx.navigateTo({
            url: '/pages/service-detail/service-detail',
        })
    },
    getUserServices() {
        let that = this;
        if (!that.data.hasMore) return;
        let pageI = ++that.data.pageIndex;
        let data = {
            pageSize: that.data.pageSize,
            pageIndex: pageI,
            userinfoId: this.data.userId,
            currentUserInfoId: app.userInfo.Id,
            parentServiceType: this.data.parentTypeId
        };
        that.setData({
            pageIndex: pageI
        });
        http.requestGet({
            url: app.globalData.serverAddress.getUserService,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            success: function(res) {
                if (res.statusCode == 200 && res.data.CodeStatus == 20000) {
                    // console.log(JSON.stringify(res.data))
                    let count = res.data.BodyContent.TotalCount;
                    let flag = (that.data.pageIndex + 1) * that.data.pageSize < count;
                    console.log(flag);
                    var newList = that.data.productList.concat(res.data.BodyContent.List);
                    newList.forEach(function(item, index) {
                        newList[index]._amount = utils.formatAmount(item.Price);
                    });
                    that.setData({
                        productList: newList,
                        hasMore: flag
                    });
                } else {
                    console.log("status!=200" + JSON.stringify(res.data))
                    that.setData({
                        hasMore: false
                    });
                }
                let msg = res.data.Message;
                Toast(msg);
            },
            fail: function(res) {
                // console.log(JSON.stringify(res));
                let msg = res.data.Message;
                Toast("请求失败，请检查网络！");
            },
            complete: function(res) {
                wx.hideLoading();
            }
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let flag = options.flag;
        // console.log(options.parmas);
        wx.showLoading({
            title: '加载中...',
        });
        // console.log(flag);
        this.setData({
            flag: flag
        });
        switch (flag) {
            case '1':
                let parmas = JSON.parse(options.parmas);
                wx.setNavigationBarTitle({
                    title: parmas.TypeName
                })
                this.setData({
                    parentTypeId: parmas.Id
                })
                this.getServiceBytype(flag);
                break;
            case '2':
                wx.setNavigationBarTitle({
                    title: options.nickName,
                });
                this.getServiceBytype(flag);
                break;
            case '3':
                let obj = JSON.parse(options.parmas);
                wx.setNavigationBarTitle({
                    title: obj.typeName,
                });
                this.setData({
                    flag: 3,
                    userId: obj.userInfoId,
                    parentTypeId: obj.typeId
                });
                this.getUserServices();
                break;
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.flag == 3) {
            this.getUserServices();
        } else {
            this.getServiceBytype();
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})